Session Expiration Alarm
You have just written the hottest web application on the Internet and your boss has come to you with a seemingly simple request, "How can I tell what users are currently logged in?" This does not seem like a particularly difficult problem. To solve it, you start off by making an array in the Application scope named currentUsers. Then, in your login code, you add the user who is logging in to the array. When the user logs out, you remove him from the array. But what about users who never explicitly log out? Their sessions simply expire. How can you ensure that those users get noted as being logged out and subsequently removed from the current user's array?
One easy solution to this problem is to use the Session Expiration Alarm (SEA) package. This package allows you to execute arbitrary ColdFusion code when a user's session expires. In the above example, you would set up a script to remove the user from the currentUsers array when the session expires.
There are many other uses for the SEA as well. For example, you could use the code to clean up temporary files or other resources that may have been allocated to users. You could also use the SEA to provide auditing for information on exactly when users log in and out of your application. The SEA can also assist in the maintenance of a site. For example, the system could notice when all users have logged out or expired, stop new users from logging in, and inform the administrator that the site is now ready for maintenance. Another creative use of the SEA is tracking user behavior. On every page a user visits, you could set up the name of that page to be delivered as an argument to your expiration handler script. When the user's session finally did expire, you would be notified of the last page they visited.
The following instructions outline how the SEA is installed and used. Note that the SEA package requires ColdFusion and J2EE to be linked. You must follow the instructions exactly to make sure that this requirement is satisfied.
Installing the SEA
Follow the steps below to install the Session Expiration Alarm package:
- Shut down your ColdFusion server.
- Copy sea.jar to wwwroot/WEB-INF/lib, where wwwroot is found under the CFusionMX installation directory.
- Copy the custom tags in the tags directory to wwwroot/cf_tags/sea/, where wwwroot is the location of your web server's web root (this directory may not be under the CFusionMX directory).
- Copy the examples directory to a location under wwwroot.
- Start up your ColdFusion server
- In the ColdFusion Administrator, select Memory Variables from the main menu. Check the boxes next to both Enable Session Variables and Use J2EE Session Variables.
- Restart your ColdFusion server.
- Browse to the examples directory that you installed under webroot and experiment with the included samples.
Using the SEA
There are a variety of ways to take advantage of the Session Expiration Alarm package. Here I demonstrate the most common approach, which involves starting the SEA process when the user logs in and stopping it when the user logs out. You should be able to add functionality that relies on the SEA without making major adjustments to your application.
Here is some typical login code:
1. <cfimport taglib="/cf_tags/sea" prefix="sea" />
2. <cflogin>
3. <cfloginuser name="test" password="test" roles="tester"/>
4. <cfscript>
5. sessionLog("login");
6. </cfscript>
7. <sea:setSessionTimeout timeout="3600"/>
8. <sea:start url = "expired.cfm">
9. <sea:param name = "id"
10. value = "! test !"/>
11. </sea:start>
12. </cflogin>
Line 1 imports the SEA package and sets up the prefix "sea" to identify tags from this package.
Line 2 signifies that the code will only be executed when the user logs in.
Lines 3–6 are examples of code that can take place during the login process. You would insert your login and authentication code at this point.
Line 7 sets the J2EE session timeout. By default, this timeout is not linked to the ColdFusion session timeout, so you must set it manually. This value is in seconds, so the code above assumes that at some point you used the cfapplication tag to set the timeout to be one hour. Failing to set the session timeout here to the ColdFusion timeout will give you unpredictable results.
Lines 8–11 actually set up the session expiration alarm. The url parameter for the start tag refers to the page that is hit when the user's session expires. In the above example, expired.cfm is requested when the user's session expires. The value of url can be either a full URL or a relative path. If it is a relative path, the value is relative to the page that used the start tag. Line 9 provides a parameter for the expired.cfm page. The param tag is provided as a convenience; you could have simply appended the id attribute and value to the expired.cfm URL. By using the param tag, you do not need to worry about URL encoding values, as the tag handles that for you. Once this code executes, the user's session contains a special token. When the user's session expires, the token is activated and an HTTP request is made to the URL provided in the start tag.
Here is some typical logout code:
1. <cflogout/> 2. <sea:stop/>
Line 1 logs the user out of the ColdFusion application and Line 2 causes the expired page to be hit explicitly, as though the user's session had expired.
For completeness, the reset tag is provided to turn off the session expiration alarm. See the sample application provided in the examples directory for a more comprehensive example.
Implementing the SEA
The Session Expiration Alarm package is built on three fundamental principles:
- A ColdFusion page can access the user's J2EE session
- Special Java objects can be placed in the J2EE session that will be notified whenever they are removed from a session
- When a user's session expires, all objects are removed from the J2EE session
These principles combine to form the basis of the SEA. The actual workings of the SEA are as follows:
When the start tag is invoked, a special Java object is created. This object knows how to listen for when it is removed from the session. Its property stipulates that when it is removed, it makes the call to the URL provided in the start tag. Next, this Java objects is placed in the user's J2EE session. When the session expires, the object is removed from the session, which causes it to request the provided URL.
One critical issue arose while developing this package. The ColdFusion and the J2EE sessions are not intimately linked; they actually expire at different times. This explains why it is critical to use the setSessionTimeout tag because it forces the Java session to have the same timeout as the ColdFusion session. Without invoking this tag, the ColdFusion session would expire and the session expiration alarm would not go off until sometime later.
With the setSessionTimeout tag, we are able to control the J2EE session timeout. It was at this point that I learned about one other issue: The J2EE and the ColdFusion sessions appear to time out using different mechanisms, which means that their timeouts are not synchronized. If you were to set both sessions to expire in 30 minutes, for example, one may expire before the other when the time is up. You cannot predict which expires first. This is an important issue because, as reported in the TechNote ColdFusion MX 6.1: Session Is Invalid Error When Using J2EE Session Management, there is a bug that occurs when the J2EE session times out before the ColdFusion session. If this ever happens, the user is put into a state where the ColdFusion application simply does not function. Therefore, it is important to try to guarantee that the ColdFusion session expires before the J2EE session.
To ensure the correct ordering of session expiration, I added a small padding of 60 seconds to the J2EE session. When you use the setSessionTimeout tag to make the J2EE session time out in 30 minutes, 60 seconds is added to this time, making the J2EE session actually expire in 31 minutes. This gives you the impression that you are setting both sessions to have the same timeout when, in reality, you are padding it by 60 seconds. Experimental evidence shows that 60 seconds is enough time to ensure that the ColdFusion session always times out before the J2EE session.